//单例模式确保类只有一个实例，并为其提供一个全局访问点
class Database {
    constructor() {
      this.connection = null;
    }
  
    static getInstance() {
      if (!Database.instance) {
        Database.instance = new Database();
      }
      return Database.instance;
    }
  
    connect() {
      // 连接到数据库
      this.connection = 'Connected';
    }
  }
  
  const db1 = Database.getInstance();
  const db2 = Database.getInstance();
  
  console.log(db1 === db2); // true
  
  db1.connect();
  
  console.log(db1.connection); // 'Connected'
  console.log(db2.connection); // 'Connected'
  